package com.demo.rydemo.d_role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.rydemo.d_role.entity.DRole;
import com.demo.rydemo.d_role.mapper.DRoleMapper;
import com.demo.rydemo.d_role.service.IDRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.rydemo.d_role.vo.DRoleVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ry
 * @since 2020-08-25
 */
@Service
public class DRoleServiceImpl extends ServiceImpl<DRoleMapper, DRole> implements IDRoleService {

    /**
     * 查询角色列表
     * @param dRoleVO
     * @return
     */
    @Override
    public IPage<DRole> getRoleList(DRoleVO dRoleVO) {
        //分页参数
        Page<DRole> page = new Page<DRole>();
        page.setCurrent(dRoleVO.getPageIndex());
        page.setSize(dRoleVO.getPageSize());

        //查询条件
        QueryWrapper<DRole> queryWrapper = new QueryWrapper<DRole>();
        if (StringUtils.isNotEmpty(dRoleVO.getRoleName()))
            queryWrapper.eq("role_name",dRoleVO.getRoleName());
        if (StringUtils.isNotEmpty(dRoleVO.getRoleCode()))
            queryWrapper.eq("role_code",dRoleVO.getRoleCode());
        if (StringUtils.isNotEmpty(dRoleVO.getStatus()))
            queryWrapper.eq("status",dRoleVO.getStatus());

        return page(page,queryWrapper);
    }

    @Override
    public Boolean addRole(DRole dRole) {
        dRole.setCreateTime(new Date());
        dRole.setRoleId(UUID.randomUUID().toString().replace("-",""));

        return save(dRole);
    }
}
